package org.blame.lotterysystem.service.activityStatus.operator;

import org.blame.lotterysystem.dao.dataObject.ActivityDO;
import org.blame.lotterysystem.dao.mapper.ActivityMapper;
import org.blame.lotterysystem.dao.mapper.ActivityPrizeMapper;
import org.blame.lotterysystem.service.dto.ConvertActivityStatusDTO;
import org.blame.lotterysystem.service.enums.ActivityPrizeEnum;
import org.blame.lotterysystem.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.stream.Collectors;

@Component
public class ActivityOperator extends AbstractActivityOperator{


    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId= convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum targetActivityStatus = convertActivityStatusDTO.getTargetActivityStatus();
        if(activityId==null || targetActivityStatus==null){
            return false;
        }
        ActivityDO activityDO = activityMapper.selectById(activityId);
        if(activityDO==null){
            return false;
        }
//        判断状态是否相同
        if(targetActivityStatus.name().equalsIgnoreCase(activityDO.getStatus())){
            return false;
        }

//        判断奖品是否抽完
        int count = activityPrizeMapper.countPrize(activityId, ActivityPrizeEnum.INIT.name());
        if(count>0){
            return false;
        }
        return true;


    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum targetActivityStatus = convertActivityStatusDTO.getTargetActivityStatus();
        try {
            activityMapper.updateStatus(activityId,targetActivityStatus.name());
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
